package com.woniuxy.controller;

import com.nimbusds.jose.JOSEException;
import com.woniuxy.entity.User;
import com.woniuxy.entity.dto.LoginDTO;
import com.woniuxy.service.UserService;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.management.relation.RoleUnresolved;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Map;

/**
 * @author dingpf
 * @version 1.0
 * @description
 * @create 2025/4/22 15:32
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/login")
    public ResponseData login(@RequestBody User user){
        try {
            LoginDTO loginDTO = userService.Login(user);
            if (loginDTO != null){
                return ResponseData.ok(loginDTO);
            } else {
                return ResponseData.fail(ResponseEnum.LOGINERROR);
            }

        } catch (JOSEException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }


    @GetMapping("/addCart/{id}/{buyNum}")
    public ResponseData addCart(@PathVariable("id") Integer id,
                                @PathVariable("buyNum") Integer buyNum  ){
        try {
            userService.addCart(id,buyNum);
            return ResponseData.ok();
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    /**
     * 查询地址信息
     * 查询购物车信息
     * @return
     */
    @GetMapping("/queryAddrAndUserCart")
    public ResponseData queryAddrAndUserCart(){
        try {
            Map map = userService.queryAddrAndUserCart();
            return ResponseData.ok(map);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

}
